﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace EPiMVC.Routing
{
    public static class PathParser
    {
        public static List<string> GetUrlSegments(string path)
        {
            string pathPart = Regex.Match(path, @"[A-Za-z0-9\-_~\/]+($|\?)").Value.Replace("?", string.Empty);
            return pathPart.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }

        public static string ToPath(this IEnumerable<string> urlSegments)
        {
            if (urlSegments.Count() == 0)
                return string.Empty;

            string path = "/";
            foreach (string urlSegment in urlSegments)
                path += string.Format("{0}/", urlSegment);

            return path;
        }

        public static string GetQueryString(string url)
        {
            if (!url.Contains("?"))
                return string.Empty;

            return Regex.Replace(url, @".*?\?", string.Empty);
        }
    }
}
